package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.PageData;
import com.woniu.entity.dto.TrainingInfoDTO;
import com.woniu.entity.vo.TrainingInfoVO;
import com.woniu.entity.vo.TrainingVO;
import com.woniu.mapper.TrainingMapper;
import com.woniu.mapper.TrainingUseMapper;
import com.woniu.mapper.UserTrainingMapper;
import com.woniu.pojo.TrainingInfo;
import com.woniu.mapper.TrainingInfoMapper;
import com.woniu.pojo.TrainingUse;
import com.woniu.pojo.UserTraining;
import com.woniu.service.ITrainingInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaosong
 * @since 2023-03-31
 */
@Service
public class TrainingInfoServiceImpl extends ServiceImpl<TrainingInfoMapper, TrainingInfo> implements ITrainingInfoService {

    @Autowired(required = false)
    private TrainingInfoMapper trainingInfoMapper;

    @Autowired(required = false)
    private UserTrainingMapper userTrainingMapper;

    @Autowired(required = false)
    private TrainingUseMapper trainingUseMapper;

    @Override
    public PageData search(Integer currentPage, Integer pageSize, TrainingInfoDTO trainingDto) {
//        开启分页
        IPage<TrainingInfoVO> page = new Page<>(currentPage,pageSize);

        IPage<TrainingInfoVO> infoVOList= trainingInfoMapper.selectall(page,trainingDto);

//         封装PageData对象
        PageData pageData = new PageData(infoVOList.getTotal(),infoVOList.getRecords());

        return pageData;
    }

//    根据id查询
    @Override
    public List<TrainingInfo> selectByid(Integer infoId) {
        QueryWrapper<TrainingInfo> wrapper = new QueryWrapper<>();
          wrapper.eq("id",infoId);
         List<TrainingInfo> infoList = trainingInfoMapper.selectList(wrapper);
        return infoList;
    }

    @Override
    public Integer updateTrainingInfo(TrainingInfo trainingInfo) {
        return trainingInfoMapper.updateById(trainingInfo);
    }

    @Override
    public boolean saveTrainingInfo(TrainingInfo trainingInfo) {
        QueryWrapper<TrainingInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("training_id",trainingInfo.getTrainingId());
        TrainingInfo info = trainingInfoMapper.selectOne(wrapper);
        if (info != null){
            return false;
        }else {
            trainingInfoMapper.insert(trainingInfo);
            return true;
        }
    }

//    展示单靶场信息

    @Override
    public TrainingVO selectInformation(Integer trainingId) {
        //        查询出 图片，标签，难度；
        TrainingVO trainingVo = trainingInfoMapper.selectInformation(trainingId);
//        查询对应的学习人数；
        QueryWrapper<TrainingUse> wrapper = new QueryWrapper();
        wrapper.eq("training_id",trainingId);
        List<TrainingUse>  userTrainings = trainingUseMapper.selectList(wrapper);
//        将人数封装到vo模型中
        trainingVo.setNumberOfChallenges(userTrainings.size());
        return trainingVo;
    }

    @Override
    public TrainingInfo getTaraingById(Integer trainingid) {
        QueryWrapper<TrainingInfo> wrapper = new QueryWrapper();
        wrapper.eq("training_id",trainingid);
         TrainingInfo trainingInfo = trainingInfoMapper.selectOne(wrapper);

        return trainingInfo;
    }
}
